const state = {
  isLogin: false,
  userInfo: {},
  avatarSrc: ''
};
const mutations = {
  CHANGEISLOGIN(state) {
    state.isLogin = !state.isLogin;
  },
  SAVEUSERINFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  CLEARUSERINFO(state) {
    state.userInfo = {};
  },
  AVATARSRC(state, avatarSrc) {
    state.avatarSrc = avatarSrc;
  }
};
const actions = {
  loginSuccess({commit}, userInfo) {
    commit("CHANGEISLOGIN");
    commit("SAVEUSERINFO", userInfo);
    localStorage.setItem("userInfo", JSON.stringify(userInfo));
  },
  identifyUser: function ({state, commit}) {
    if (JSON.stringify(state.userInfo) !== "{}") return;
    let userInfo = JSON.parse(localStorage.getItem("userInfo"));
    commit("SAVEUSERINFO", userInfo);
    commit("CHANGEISLOGIN");
  },
  logout({commit}) {
    commit("CHANGEISLOGIN");
    commit("CLEARUSERINFO");
    localStorage.removeItem("userInfo");
    return Promise.resolve({code: 200, message: "成功退出"});
  }
};
const getters = {
  userId(state) {
    return state.userInfo.userId || '';
  }
};

export default {
  state,
  mutations,
  actions,
  getters,
};
